package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vo.AdUserVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

import static com.heima.common.constants.admin.AdminConstants.AD_USER_ISSTATUS;

@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper,AdUser> implements AdUserService {
    @Override
    public ResponseResult login(AdUserDto dto) {
        //校验参数
        if(dto==null||StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断用户是否存在
        AdUser one = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, dto.getName()));
        if(one==null){
            CustException.cust(AppHttpCodeEnum.AD_USER_DATA_NOT_EXIST);
        }
        //判断用户状态
        if(one.getStatus()!=AD_USER_ISSTATUS){
            CustException.cust(AppHttpCodeEnum.AD_USER_STUTAS_INVLID);
        }
        //判断密码是否一致
        String passwordReal = one.getPassword();
        String password = dto.getPassword() + one.getSalt();
        String passwordMd5 = DigestUtils.md5Hex(password.getBytes());
        if(!passwordMd5.equals(passwordReal)){
            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //密码验证正确，修改用户最近登录时间
        one.setLoginTime(new Date());
        updateById(one);
        //将id封装成token
        String token = AppJwtUtil.getToken(Long.valueOf(one.getId()));
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        AdUserVo adUserVo = new AdUserVo();
        BeanUtils.copyProperties(one,adUserVo);
        map.put("user",adUserVo);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult register(AdUser user) {
        //验证参数
        if(user==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if(StringUtils.isBlank(user.getName())||StringUtils.isBlank(user.getPassword())){
            CustException.cust(AppHttpCodeEnum.NAME_PASSWORD_IS_NULL);
        }
        String salt = RandomStringUtils.randomAlphanumeric(8);
        String passwordReal = DigestUtils.md5Hex((user.getPassword() + salt).getBytes());
        //设置参数
        user.setPassword(passwordReal);
        user.setCreatedTime(new Date());
        user.setSalt(salt);
        user.setStatus(AD_USER_ISSTATUS);
        save(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
